package indi.zhifa.study2025.test.rabbitmq.route.consumer.handler;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import indi.zhifa.study2025.test.rabbitmq.common.Constant;
import indi.zhifa.study2025.test.rabbitmq.common.entity.ItemInfoChangeMsg;
import indi.zhifa.study2025.test.rabbitmq.common.entity.OrderMsg;
import indi.zhifa.study2025.test.rabbitmq.common.entity.SalesChangeMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitMQListener {

    @RabbitListener(queues = Constant.ROUTE_QUE_NAME1)
    public void queue1ReceiveMessage(Message msg, Channel channel) {

        String key = msg.getMessageProperties().getReceivedRoutingKey();
        log.info("que1 监听到key "+key);
        switch (key){
            case Constant.ITEM_INFO_CHANGE_KEY -> {
                ItemInfoChangeMsg itemInfoChangeMsg = JSON.parseObject(msg.getBody(), ItemInfoChangeMsg.class);
                log.info("que1 Received message: 商品 id" + itemInfoChangeMsg.getItemId() +
                        "，名字从 "+itemInfoChangeMsg.getItemOrgName()+" 变化为 "+itemInfoChangeMsg.getItemOrgName());
            }
            case Constant.SALES_CHANGE_KEY -> {
                SalesChangeMsg salesChangeMsg = JSON.parseObject(msg.getBody(), SalesChangeMsg.class);
                log.info("que2 Received message: 销售员改名" + salesChangeMsg.getOrgName()+
                        "->"+salesChangeMsg.getNewName());
            }
        }
    }

    @RabbitListener(queues = Constant.ROUTE_QUE_NAME2)
    public void queue2ReceiveMessage(Message msg, Channel channel) {
        String key = msg.getMessageProperties().getReceivedRoutingKey();
        log.info("que2 监听到key "+key);
        switch (key){
            case Constant.ITEM_INFO_CHANGE_KEY -> {
                ItemInfoChangeMsg itemInfoChangeMsg = JSON.parseObject(msg.getBody(), ItemInfoChangeMsg.class);
                log.info("que2 Received message: 商品 id" + itemInfoChangeMsg.getItemId() +
                        "，名字从 "+itemInfoChangeMsg.getItemOrgName()+" 变化为 "+itemInfoChangeMsg.getItemOrgName());
            }
            case Constant.BILL_CONSIGN_FINISH -> {
                OrderMsg orderMsg = JSON.parseObject(msg.getBody(), OrderMsg.class);
                log.info("que1 Received message: 订单完成，code为" + orderMsg.getCode()+
                        ", 金额为"+orderMsg.getAmount()+", 完成时间为"+orderMsg.getBillTime());
            }
        }
    }

}
